import { tokenManager } from "./token";
import { setShowModal,setFrame } from "./eventBus";
import { shopItemService } from "@/services/shop_item";


export var userManager={
    getUserInfo:function(){
        const user = localStorage.getItem('user');
        if(user){
            return JSON.parse(user)
        }
        return null
    },
    setUserInfo:function(user,student,teacher){
        localStorage.setItem('user',JSON.stringify(user))
        if(student){
            localStorage.setItem('student',JSON.stringify(student))
        }
        if(teacher){
            localStorage.setItem('teacher',JSON.stringify(teacher))
        }
    },
    removeUserInfo:function(){
        localStorage.removeItem('user')
    },
    removeStudentInfo:function(){
        localStorage.removeItem('student')
    },
    removeTeacherInfo:function(){
        localStorage.removeItem('teacher')
    },
    getRole:function(){
        const user = this.getUserInfo();
        if(user){
            return user.userType === 0 ? 'student' : 'teacher';
        }
        return null
    },
    getStudent:function(){
        const student = localStorage.getItem('student');
        if(student){
            return JSON.parse(student)
        }
        return null

        
    },
    getTeacher:function(){
        const teacher = localStorage.getItem('teacher');
        if(teacher){
            return JSON.parse(teacher)
        }
        return null
    },
    getUserImage(){
        const userInfo = this.getUserInfo();
        if (!userInfo) {
            // 用户未登录，返回默认头像路径
            return 'https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/02/d608cb75-1f1c-48d1-819c-03152564aa22.png'; // 假设 default.png 是通用的默认头像
        }

        if(this.getRole() === 'student'){
            const studentInfo = this.getStudent();
            return studentInfo ?  studentInfo.avatar : 'https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/02/d608cb75-1f1c-48d1-819c-03152564aa22.png';
        }
        if(this.getRole() === 'teacher'){
            const teacherInfo = this.getTeacher();
            return teacherInfo ?  teacherInfo.avatar : 'https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/02/d608cb75-1f1c-48d1-819c-03152564aa22.png';
        }
        return 'https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/02/d608cb75-1f1c-48d1-819c-03152564aa22.png'; // 默认通用头像，以防万一
    },
    getServerUrl(){
        return 'http://localhost:8080'
    },
    getUserName(){
        if(this.getUserInfo()){
            return this.getUserInfo().username
        }
        return ''
    },
    getPreferences(){
        if(this.getRole() === 'student'){
            const preferences = this.getStudent().hobbies
             if(preferences){
            return preferences.split(',')
        }
        }
        return []  
    },
    getUserId(){
        const user = this.getUserInfo();
        return user ? user.id : null;
    },
    getUserEmail(){
        if(this.getUserInfo()){
            return this.getUserInfo().email
        }
        return ''
    },
    getUserPassword(){
        return this.getUserInfo().password
    },
    getBirthDate(){
        const birthDate=this.getRole() === 'student' ? this.getStudent().birthDate : this.getTeacher().birthDate
        
        return birthDate.substring(0,10)
    },
    logout(){
        //到首页
       
        this.removeUserInfo()
        this.removeStudentInfo()
        this.removeTeacherInfo()
        tokenManager.removeToken()
        
    },
    isStudent() {
      return this.getUserType() === 0;
    },
    isTeacher() {
     return this.getUserType() === 1;
    },
    getMajor() {
  if (this.isTeacher()) {
    const teacher = this.getTeacher();
    return teacher?.major || '';
  }
  return ''; // 非教师返回空字符串
    },
    getUserType(){
        return this.getUserInfo().userType
    },
    toLogin(){
        setShowModal(true)
    },
    setUserFrameUrl(item_id)
    {
       
       
     const imageAvater=[
       {
           id:'0233b599-ac62-4a2f-95fb-cc5fdec6ded5',
           imageUrl:'https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/07/dafa75e9-1538-46ab-93a9-64a2b13a2f85.png',
        },
       {
           id:'02367c3b-4b0c-4f61-905e-1365e4c23d26',
           imageUrl:'https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/07/aac77aba-e961-47f3-abac-2fa454ba3a2f.png',
       },
    ]
    let shopItems=[]
    shopItemService.getShopItems((res)=>{
        if(res.code==1000){
            shopItems=res.data


     //根据id查找
    const frame = shopItems.find(item => item.id === item_id);
    if(frame){

    localStorage.setItem("frame",JSON.stringify(frame.image))
    setFrame(frame.image)
    }
    else{
        localStorage.setItem("frame","0")
    }

        }
    })






    


    },
    getUserFrameUrl(){
        const frame = localStorage.getItem("frame");
        if(frame){
            return JSON.parse(frame)
        }
        return "0"
    }




    

    
    

}